<template>
  <div>
    <div style="padding-top: 10px; margin-left: 10px">
      <div>
        <el-button
          v-permissions="{ permission: ['add:ReceptionSetting'] }"
          type="primary"
          @click="addReception"
        >
          新增
        </el-button>
      </div>
      <el-form
        class="demo-form-inline"
        :inline="true"
        :model="formInline"
        style="margin-top: 10px"
      >
        <el-form-item label="名称">
          <el-input v-model="formInline.name" placeholder="名称" />
        </el-form-item>
        <!-- <el-form-item label="活动区域">
          <el-select v-model="formInline.region" placeholder="活动区域">
            <el-option label="区域一" value="shanghai" />
            <el-option label="区域二" value="beijing" />
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary">查询</el-button>
        </el-form-item>
      </el-form>

      <div style="margin-top: 10px">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="名称" prop="menu_name" />
          <el-table-column label="操作" prop="address">
            <template slot-scope="scope">
              <el-button
                v-permissions="{ permission: ['edit:ReceptionSetting'] }"
                @click="upd(scope.row)"
              >
                修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <el-dialog
      :before-close="handleClose"
      title="接诊设置"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <span>
        <el-form
          ref="refname"
          class="demo-form-inline"
          :inline="true"
          :model="form"
          style="margin-top: 10px"
        >
          <el-form-item label="名称">
            <el-input v-model="form.name" placeholder="名称" />
          </el-form-item>

          <el-form-item>
            <el-cascader
              ref="cascader"
              v-model="form.value"
              :options="options"
              :props="defaultProps"
              @change="receptonChange"
            />
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getTreatmentSetting,
    saveTreatmentSetting,
    treatmentSetting,
  } from '@/api/reception'
  export default {
    data() {
      return {
        total: 1,
        formInline: {
          name: '',
          pagesize: 10,
          page: 1,
        },
        form: {
          name: '',
          value: [],
        },
        tableData: [],
        dialogVisible: false,
        options: [],
        defaultProps: {
          value: 'id',
          label: 'item_name',
        },
      }
    },
    mounted() {
      this.init()
    },
    beforeMount() {},
    methods: {
      handleCurrentChange(val) {
        const _this = this
        _this.formInline.page = val
        this.init()
      },
      upd(e) {
        // this.form.value.push(e.menu_key)
        // this.form.value.push(e.menu_value)

        this.form.value[0] = e.menu_key
        this.form.value[1] = e.menu_value
        this.form.name = e.menu_name
        this.form.id = e.id
        console.log(this.form)
        this.dialogVisible = true
        console.log(e)
      },
      receptonChange(e) {
        console.log(this.$refs['cascader'])
        console.log(e)
      },
      addReception() {
        this.form.id = ''
        // this.form.name = ''
        // this.form.value = []
        this.dialogVisible = true
      },
      init() {
        getTreatmentSetting().then((e) => {
          e.data.forEach((k) => {
            k.item_name = k.menu_name
            k.id = k.menu_key
          })
          this.options = e.data
        })
        treatmentSetting(this.formInline).then((e) => {
          this.tableData = e.data.data
          this.total = e.data.total
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(() => {
            done()
          })
          .catch(() => {})
      },
      submit() {
        if (this.form.name) {
          const data = {
            menu_key: this.form.value[0],
            menu_value: this.form.value[1],
            name: this.form.name,
            id: this.form.id,
          }
          console.log(data)
          saveTreatmentSetting(data).then(() => {
            this.dialogVisible = false
            this.init()
          })
        }
      },
    },
  }
</script>

<style scoped></style>
